from scipy import array

def shiftfilter(poly):
    """ Produces a shifted version of only the odd elements of
        a characteristic polynomial. Used in Hurwitz checks.
    """
    result = []
    l = len(poly)
    i = 1

    while l-i>0:
        result.append(poly[i])
        i += 1
        if l-i>0:
            result.append(0)
            i += 1
    result.append(0)
    
    return array(result, float)


def ishurwitz(poly):
    """ Tests whether a polynomial is Hurwitz. """
    if len(poly) < 2: return True
    p0,p1 = poly[0:2]

    if p0*p1 <= 0: return False
    if len(poly) == 2: return True

    recursor = poly - p0*shiftfilter(poly)/p1
##    print poly,'-',p0,'/',p1,'*',shiftfilter(poly),'=',recursor
    return ishurwitz(recursor[1:])
